
/// This file calculates the proxy for child costs in the UK by looking at the relative price of the service sector to the CPI



use "$base/InputData/10SD_jan15.dta",clear
rename Country countrycode
rename Year year
keep if countrycode=="GBR"
keep if Variable=="VA" || Variable=="VA_Q05"
gen IND= MIN+ MAN+ PU+ CON
gen SER=SUM-(IND+AGR)

keep year AGR IND SER SUM Variable

reshape wide AGR SUM IND SER, i(year) j(Variable) string



tempfile returnhere
save `returnhere'

///----------------------------
keep if year==1965
keep year *_*
sort year
tempfile data1965
save `data1965'
///------------------------

use `returnhere',clear
gen ak_sh=AGRVA_Q05/SUMVA_Q05
gen ik_sh=INDVA_Q05/SUMVA_Q05
gen sk_sh=SERVA_Q05/SUMVA_Q05
keep year *_sh
drop if ak==.
sort year
tempfile sectorSharesConstant1949_2009
save `sectorSharesConstant1949_2009'


use `returnhere',clear
gen ac_sh=AGRVA/SUMVA	
gen ic_sh=INDVA/SUMVA
gen sc_sh=SERVA/SUMVA
keep year *_sh
drop if ac==.
tempfile sectorSharesCurrent1949_2009
save `sectorSharesCurrent1949_2009'






/// 1855-1965 constant price (2005) sector shares

clear
import excel using "$base/InputData/hna_gbr_09.xls", sheet("VA-Ki")  
drop if _n<=3
drop C
drop DK- GS

keep if B=="A; B"|| B=="C; D; E; F" || B=="GDP" //|| B=="ID"

drop if _n>=4
drop B
rename (D- DJ) d=
rename d* d#, renumber


reshape long d, i(A) j(year)
replace year=year+1854 
rename A sector

replace sector="A" if sector=="AGRICULTURE, HUNTING, FORESTRY AND FISHING"
replace sector="I" if sector=="TOTAL INDUSTRY"
replace sector="T" if sector=="TOTAL GDP"

reshape wide d, i(year) j(sector) string
rename d* *


//Re-base data to 1965
gen tempA=A if year==1965
gen tempI=I if year==1965
gen tempT=T if year==1965

egen tempAm=min(tempA)
egen tempIm=min(tempI)
egen tempTm=min(tempT)

replace A =A/tempAm
replace I=I/tempIm
replace T=T/tempTm
drop temp*

//Convert indices into values
merge year using `data1965'

drop SERVA_Q05 _merge

egen tempAm=min(AGRVA_Q05)
egen tempIm=min(INDVA_Q05)
egen tempTm=min(SUMVA_Q05)

replace A =A*tempAm
replace I=I*tempIm
replace T=T*tempTm

drop temp* *_*

gen S=T-(A+I)
gen ak_sh=A/T
gen ik_sh=I/T
gen sk_sh=S/T

keep year *_sh


//Combine both sets of data to create 1855-2009 constant price (2005) sector shares

drop if year>=1949
sort year
merge year using `sectorSharesConstant1949_2009'
drop _merge
sort year




tempfile sectorSharesConstant1855_2009
save `sectorSharesConstant1855_2009'
//-------------------------------------------------------------------------

/// Read in UK current value added shares from Buera Kaboski paper (who are essentially using Mitchell) and combine with GGDC data post 1960
import excel using "$base/InputData/2000submitteddata_allcountries.xls", sheet("UK")  clear

drop if _n<=2
drop if _n>=208

drop if _n>=161 //Drop post 1960 data
//drop if _n>=150



keep A-D
destring A, gen(year)
destring B, gen(ac_sh)
destring C, gen(ic_sh)
destring D, gen(sc_sh)

keep year *_*
sort year


merge year using `sectorSharesCurrent1949_2009'
drop _merge
sort year

tempfile sectorSharesCurrent1800_2011
save `sectorSharesCurrent1800_2011'







//-------------------------------------------------------------------------

// Import NGDP from Millenium Data

clear
import excel using "$base/InputData/a-millennium-of-macroeconomic-data-for-the-uk.xlsx", sheet("A9. Nominal GDP (A)")  
keep A H
drop if _n<=17
drop if _n>=318
gen year=_n+1699
drop A
destring H, gen(ngdp)
drop H
sort year

save "$base/Outputs/Data/NGDPGB_a-millennium-of-macroeconomic-data-for-the-uk.dta",replace




// Import RGDP from Millenium Data

clear
import excel using "$base/InputData/a-millennium-of-macroeconomic-data-for-the-uk.xlsx", sheet("A8. UK Real GDP(A)")  
keep A K
drop if _n<=17
drop if _n>=318
gen year=_n+1699
drop A
destring K, gen(rgdp)
drop K
sort year

save "$base/Outputs/Data/RGDPGB_a-millennium-of-macroeconomic-data-for-the-uk.dta",replace



// Merge ngdp and rgdp and convert rgdp to base 2005
use "$base/Outputs/Data/RGDPGB_a-millennium-of-macroeconomic-data-for-the-uk.dta",clear
merge year using  "$base/Outputs/Data/NGDPGB_a-millennium-of-macroeconomic-data-for-the-uk.dta"
drop _merge

gen temp=rgdp if year==2005
egen temprgdp2005 =min(temp)
gen rgdp2005index= rgdp/temprgdp2005
drop temp*

gen temp=ngdp if year==2005
egen tempngdp2005 =min(temp)
gen rgdp2005= rgdp2005index*tempngdp2005
drop temp*

keep year ngdp rgdp2005
rename rgdp2005 rgdp 
sort year 

tempfile ngdprgdp
save `ngdprgdp'


//------ Read if current and constant value added shares and merge with nominal and real gdp measures


use `sectorSharesCurrent1800_2011',clear
merge year using  `sectorSharesConstant1855_2009'
drop _merge
sort year



merge year using `ngdprgdp'
drop _merge
drop if _n>=211


///Interpolation
tsset year
ipolate ac_sh year, gen(ac_sh2)
ipolate ic_sh year, gen(ic_sh2)
ipolate sc_sh year, gen(sc_sh2)


ipolate ak_sh year, gen(ak_sh2)
ipolate ik_sh year, gen(ik_sh2)
ipolate sk_sh year, gen(sk_sh2)

replace ac_sh=ac_sh2
replace ic_sh=ic_sh2
replace sc_sh=sc_sh2

replace ak_sh=ak_sh2
replace ik_sh=ik_sh2
replace sk_sh=sk_sh2
drop *2

///-------------------



gen ac_va=ac_sh*ngdp
gen ic_va=ic_sh*ngdp
gen sc_va=sc_sh*ngdp


gen ak_va=ak_sh*rgdp
gen ik_va=ik_sh*rgdp
gen sk_va=sk_sh*rgdp



gen ps_new=sc_va/sk_va
gen pi_new=ic_va/ik_va
gen p_new=(ac_va+ic_va+sc_va)/(ak_va+ik_va+sk_va)

//gen rel_ps_pi=ps/pi
//gen rel_ps_p=ps/p

keep year ps_new pi_new p_new

sort year
tempfile pricesnew
save `pricesnew'


//Seems to be jump at 1960 - This is due to the fact the the current price ten sector GGDC data is a bit different to the BK value added data and since I am connecting
//these at 1960 we get a bit of a jump

//----------------------------------------------------
//----------------------------------------------------
//----------------------------------------------------
//Read in 1270-1870 prices


clear
import excel using "$base/InputData/a-millennium-of-macroeconomic-data-for-the-uk.xlsx", sheet("A6. English GDP(O) 1270-1700")  
keep A I-L
drop if _n<=10

destring A, gen(year)
destring I, gen(pa)
destring J, gen(pi)
destring K, gen(ps)
destring L, gen(p)
drop A I J K L
drop if year==1700
sort year


tempfile prices1270_1699
save `prices1270_1699'


clear
import excel using "$base/InputData/a-millennium-of-macroeconomic-data-for-the-uk.xlsx", sheet("A7. GB GDP(O) 1700-1870")  
keep A I-L
drop if _n<=8


destring A, gen(year)
destring I, gen(pa)
destring J, gen(pi)
destring K, gen(ps)
destring L, gen(p)
drop A I J K L
sort year

merge year using `prices1270_1699'
drop _merge
sort year

save "$base/Outputs/Data/1270-1870prices.dta",replace




//-------


use "$base/Outputs/Data/1270-1870prices.dta",clear

//	gen rel_ps_pi_old=ps/pi
//	gen rel_ps_p_old=ps/p
	
//	drop p*
merge year using `pricesnew'
drop _merge



sort year
qui forval i = 1854(-1)1270 {
    replace ps_new = ps_new[_n+1]*(ps[_n]/ps[_n+1]) if year == `i'
	replace pi_new = pi_new[_n+1]*(pi[_n]/pi[_n+1]) if year == `i'
	replace p_new = p_new[_n+1]*(p[_n]/p[_n+1]) if year == `i'
}

/*
gen relpsp=ps_new/p_new
qui levelsof relpsp if year==1270, local(relpsp_1270)
replace relpsp = relpsp / `relpsp_1270'
*/

drop ps pi p pa
rename ps_new ps
rename pi_new pi
rename p_new p

sort year

sort year
tempfile sectoralprices
save `sectoralprices'


//----------------------------------------------------
//----------------------------------------------------
//----------------------------------------------------
//Read in cpi and rpi index


clear
import excel using "$base/InputData/a-millennium-of-macroeconomic-data-for-the-uk.xlsx", sheet("A47. Wages and prices")  
keep A D G
drop if _n<=6

destring A, gen(year)
destring D, gen(cpi)
destring G, gen(rpi)
drop A D G
drop if year==.
sort year


save "$base/Outputs/Data/1209-1217cpi_rpi.dta",replace

//-------


use "$base/Outputs/Data/1209-1217cpi_rpi.dta",clear

merge year using `sectoralprices'
drop _merge pi p
drop if ps==.
sort year

gen rel_ps_cpi=ps/cpi
gen rel_ps_rpi=ps/rpi
drop rpi cpi ps


//HP smooth Data
gen long yearlong = year  // Convert year to a long variable
tsset yearlong, yearly  // Declare the data as yearly time series
hprescott rel_ps_cpi, stub(cpi_) smooth(100)  // Apply the HP filter to rel_ps_cpi
hprescott rel_ps_rpi, stub(rpi_) smooth(100)  // Apply the HP filter to rel_ps_rpi
drop yearlong  cpi__rel_ps_cpi_1 rpi__rel_ps_rpi_1 // If you want, you can drop the yearlong variable

rename cpi__rel_ps_cpi_sm_1 rel_ps_cpi_hp
rename rpi__rel_ps_rpi_sm_1 rel_ps_rpi_hp


//Re-base at 1800
foreach var in rel_ps_cpi rel_ps_rpi rel_ps_cpi_hp rel_ps_rpi_hp{
    qui levelsof `var' if year==1800, local(myval)
    replace `var' = `var' / `myval'
}



export delimited using "$base/Outputs/Data/ChildPrices.csv", replace
